package com.imisty.servlet;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imisty.bean.Person;
import com.imisty.service.UserService;
import com.imisty.service.impl.UserServiceImpl;
import com.imisty.util.PageUtil;
import org.apache.ibatis.plugin.Intercepts;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/servlet/UserServlet2")
public class UserServlet2 extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("all")){
            selectAllUsers(req,resp);
        }
    }

    private void selectAllUsers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String num = req.getParameter("num");
        if (null == num){
            num =  "1" ;
        }
        Page<?> page = PageHelper.startPage(Integer.parseInt(num),5);

        List<Person> persons = userService.getAllUserByPageHelper();

        PageInfo<?> pageHelper = page.toPageInfo();

        req.setAttribute("persons",persons);
        req.setAttribute("pagehelper",pageHelper);
        req.getRequestDispatcher("/persons.jsp").forward(req,resp);

    }
}
